package ucap.com.lesanapp.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import ucap.com.lesanapp.R;
import ucap.com.lesanapp.base.BaseDialog;

/**
 * Created by wekingwang on 16/10/17.
 */

public class UpDateDialog extends BaseDialog {
    @Bind(R.id.btn_update)
    Button btnUpdate;
    @Bind(R.id.tv_version_name)
    TextView tvVersionName;
    @Bind(R.id.tv_update_content)
    TextView tvUpdateContent;
    @Bind(R.id.btn_cancel)
    Button btnCancel;
    String versionName;
    String updateContent;
    String path;
    String pathUri;

    protected UpDateDialog(Context context, String versionCode, String versionName, String updateContent, String path) {
        super(context);
        this.versionName = versionName;
        this.updateContent = updateContent;
        this.path = path;
        pathUri = Environment.getExternalStorageDirectory().getPath() + "/lesanAppForAndroid" + versionCode + ".apk";
    }

    public static UpDateDialog showDialog(Context context, String versionCode, String versionName, String updateContent, String path) {
        UpDateDialog upDateDialog = new UpDateDialog(context, versionCode, versionName, updateContent, path);
        upDateDialog.show();
        return upDateDialog;
    }

    @Override
    public void setView() {
        setContentView(R.layout.dialog_update);
        ButterKnife.bind(this);
    }

    @Override
    public void setListener() {
        btnUpdate.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void setData() {
        tvVersionName.setText(versionName);
        tvUpdateContent.setText(updateContent);
    }


    @Override
    public void setClick(View v) {
        if (v == btnUpdate) {
            HttpUtils utils = new HttpUtils();
            utils.download(path, pathUri, true, true, new RequestCallBack<File>() {
                @Override
                public void onSuccess(ResponseInfo<File> responseInfo) {

                    appStart(responseInfo.result.getPath());
                }

                @Override
                public void onStart() {
                    Toast.makeText(getContext(), "正在后台下载更新...", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onFailure(HttpException error, String msg) {
                    Log.i("updatePath", msg);
                    if (new File(pathUri).exists()) {
                        appStart(pathUri);
                    } else {
                        Toast.makeText(getContext(), "下载失败..." + msg, Toast.LENGTH_SHORT).show();
                    }
                }
            });
            Log.i("updatePath", path);
        } else if (btnCancel == v) {
            if (onCancel != null)
                onCancel.onCancel();
            dismiss();
        }
        dismiss();

    }

    private OnCancel onCancel;

    public void setOnCancel(OnCancel onCancel) {
        this.onCancel = onCancel;
    }

    /**
     * 取消
     */
    public interface OnCancel {
        void onCancel();
    }

    /**
     * 安装app
     *
     * @param uri
     */
    public void appStart(String uri) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
/* 调用getMIMEType()来取得MimeType */
        String type = "application/vnd.android.package-archive";
/* 设置intent的file与MimeType */
        intent.setDataAndType(Uri.parse("file://" + uri), type);
        getContext().startActivity(intent);
    }
}
